package com.xh.bussiness.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.xh.bussiness.system.bean.dto.ParamBean;
import com.xh.bussiness.system.bean.request.ParamRequest;
import com.xh.bussiness.system.service.IParamService;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.core.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author daiwei
 * @since 2018-08-11
 */
@Controller
@RequestMapping("/system/param")
public class ParamController extends BaseController {

    @Autowired
    private IParamService paramService;


    /**
     * 跳转列表页面
     * @return
     */
    @RequestMapping("/toList")
    public String toList(){
        return "admin/system/param/list";
    }


    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @RequestMapping("/queryPage")
    @ResponseBody
    public AjaxResult queryPage(XhPage page, ParamRequest bean){
        page=paramService.queryParamPage(page,bean);
        System.out.println(page.getTotal());
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,page);
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @RequestMapping("/exportAll")
    @ResponseBody
    public AjaxResult exportAll(ParamRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 导出选中项
     * @param ids
     * @return
     */
    @RequestMapping("/exportSelect")
    @ResponseBody
    public AjaxResult exportSelect(List<String> ids){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 前往维护页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "admin/system/param/add";
    }

    /**
     * 前往维护页面
     * @return
     */
    @RequestMapping("/toModify/{id}")
    public String toModify(@PathVariable String id){
        return "admin/system/param/modify";
    }

    /**
     * 前往查看页面
     * @param id
     * @return
     */
    @RequestMapping("/toView/{id}")
    public String toView(@PathVariable String id){
        System.out.println(JSONObject.toJSON(paramService.getById(id)));
        return "admin/system/param/view";
     }

    /**
     * 新增和保存
     * @param param 实体对象
     * @return
     */
    @RequestMapping(value="/saveOrUpdate",method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public AjaxResult saveOrUpdate(ParamBean param){
        if(param.getId()==null){
            paramService.insertObj(param);
        }else{
            paramService.updateObj(param);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @RequestMapping("/deleteById/{id}")
    @ResponseBody
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        paramService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        paramService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


}

